package com.study.web.shiro.session;

import java.io.Serializable;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;

import com.study.common.utils.LoggerUtils;
/**
 * Session 操作
 * @author yaojinshan
 * @since 2017-09-15 14:24
 * 
 */
public class CustomShiroSessionDAO extends AbstractSessionDAO { 
	@Resource(name="redisShiroSessionRepository")
    private ShiroSessionRepository shiroSessionRepository;  
  
    public void update(Session session) throws UnknownSessionException {  
    	shiroSessionRepository.saveSession(session);  
    }  
  
    public void delete(Session session) {  
        if (session == null) {  
        	LoggerUtils.error(getClass(), "Session 不能为null");
            return;  
        }  
        Serializable id = session.getId();  
        if (id != null) {
        	shiroSessionRepository.deleteSession(id);  
        }
    }  
  
    public Collection<Session> getActiveSessions() {  
        return shiroSessionRepository.getAllSessions();  
    }  
  
    @Override  
    protected Serializable doCreate(Session session) {
    	System.out.println("生成sesisonid");
        Serializable sessionId = this.generateSessionId(session);  
        this.assignSessionId(session, sessionId);  
        shiroSessionRepository.saveSession(session);  
        return sessionId;  
    }  
  
    @Override  
    protected Session doReadSession(Serializable sessionId) {  
        return shiroSessionRepository.getSession(sessionId);  
    } 
 }
